Ext.ns("CRM.SPComp");

(function(){
	var contactValidationCompIds = new Array("contactFirstName","contactTitle","contactEmail","phoneWork");
	
	var actionTypeStore=CRM.commons.createComboStore('activityStoreid','activity/actionTypes','');
	var stageStore=CRM.commons.createComboStore('stageStoreid','lead/stageTypes','');
	var statusStore=CRM.commons.createComboStore('statusStoreid','activity/statusTypes','');
	var priorityStore=CRM.commons.createComboStore('priorityStoreid','activity/priorityTypes','');
	var assignedToStore=CRM.commons.createComboStore('assignedToStoreid','activity/assignedTo','');
	var opportunitiesTitleStore=CRM.commons.createComboStore('opportunitiesTitleStoreid','activity/opportunitiesTitles','');    
	
	var win;
	
	CRM.SPComp.createContactForm = function(leadId, contactId){
	      		
	      		var contactDetailsPanel = {
	                    xtype       : 'fieldset',
	                    id          : 'contactPanel',
	                    title       : 'Contact Information',
	                    height      : 250,
	                    width       : 550,
	                    labelWidth  : 120,
	                    layout      : 'form',
	                    items       : [
										{
										    xtype   : 'hidden',
										    id      : 'contactLeadId',
										    value	: leadId
										 },
						                 {
											xtype	: 'hidden',
											id		: 'contactId',
											value	: contactId
						                },	                                   
	                                    {
	                                      xtype     : 'compositefield',
	                                      fieldLabel: 'Name<font color=red>*</font>',
	                                      items     : [
	                                                    {
	                                                        xtype   : 'textfield',
	                                                        id      : 'contactFirstName',
	                                                        width   :  180,
	                                                        allowBlank : false
	                                                     },
	                                                     {
	                                                        xtype   : 'textfield',
	                                                        id      : 'contactMiddleInitial',
	                                                        width   :  30
	                                                     },
	                                                     {
	                                                        xtype   : 'textfield',
	                                                        id      : 'contactLastName',
	                                                        width   :  180
	                                                     }
	                                                   ]
	                                    },
	                                    {
	                                        xtype       : 'textfield',
	                                        id          : 'contactTitle',
	                                        fieldLabel  : 'Title<font color=red>*</font>',
	                                        width       : 180
	                                    },
	                                    {
	                                        xtype       : 'textfield',
	                                        id          : 'contactEmail',
	                                        fieldLabel  : 'Email<font color=red>*</font>',
	                                        width       : 400
	                                    },
	                                    {
	                                        xtype       : 'textfield',
	                                        id          : 'phoneWork',
	                                        fieldLabel  : 'Phone (Work)<font color=red>*</font>',
	                                        width       : 220
	                                    },
	                                    {
	                                        xtype       : 'textfield',
	                                        id          : 'phoneHome',
	                                        fieldLabel  : 'Phone (Home)',
	                                        width       : 220
	                                    },
	                                    {
	                                        xtype       : 'textfield',
	                                        id          : 'phoneMobile',
	                                        fieldLabel  : 'Mobile No.',
	                                        width       : 220
	                                    },
	                                    {
	                                        xtype       : 'textfield',
	                                        id          : 'phoneFax',
	                                        fieldLabel  : 'Fax',
	                                        width       : 220
	                                    }
	                                  ]
	                };

               

                var popUpContactPanel = new Ext.FormPanel({
	            id          : 'popUpContactPanel',
	            height      : 280,
	            width       : 555,
	            frame       : false,
	            layout      : 'hbox',
	            items       : [contactDetailsPanel],
	            bbar        : {
				                xtype       : 'toolbar',
				                buttonAlign : 'right',
				                items       : [
											   {
											    xtype   : 'button',
											    id		: 'addButton',
											    text    : 'Save',
											    handler : function(){
											    				
											    				var actionValue = Ext.getCmp('addButton').getText();
											    				//addContact(actionValue);
											    				
											    				var form = Ext.getCmp('popUpContactPanel');
											    				var validationSuccess = simpleFormValidation(form, contactValidationCompIds);
											    				if(validationSuccess == 'true'){
											    					addContact(actionValue);
											    				}
											    			}
											   },
											   '-',
				                               {
				                                xtype   : 'button',
				                                id		: 'cancelButton',
				                                text    : 'Cancel',
				                                handler	: closeContactWindow
				                               }
				                            ]
	            		}
	        });                

       			win = new Ext.Window({
            	title		: 'Create Additional Contact',
            	id			: 'popUpContactWindow',
            	width		: 570,
            	height		: 315,
            	border		: true,
            	plain		: false,
            	closable	: true,
            	collapsible : true,
            	animCollapse: true,
            	draggable 	: true,
            	maximizable : false,
            	//layout		: 'border',
            	onEsc		: true,
            	model		: true,
            	items		: [popUpContactPanel]
        		});
            win.show(this);
            if(contactId != 0){
            	Ext.getCmp('addButton').setText('Update');
            	showContactDetails(contactId);
            }
            validateFormOnLoad(popUpContactPanel, contactValidationCompIds); 
            return win;
		};
		


		
		function addContact(action){
		   var formPanel = Ext.getCmp('popUpContactPanel');
                
  	       var values = formPanel.getForm().getValues();
  	       var data = Ext.encode(values);
  	       var selectedLeadId = Ext.getCmp('contactLeadId').getValue();
  	       var selectedContactId = Ext.getCmp('contactId').getValue();
      	   Ext.Ajax.request({
      		   url		: 'lead/saveContact?action='+action+"&leadId="+selectedLeadId+"&contactId="+selectedContactId,
      		   method	: 'POST',
      		   success	: function(result, request){
      			  			var jsonData = Ext.util.JSON.decode(result.responseText); 
      			  			var responsedContactId = jsonData.data;
      			   			alert("Contact "+action+"d successfully!");
	      			   		var selectedId = null;
	   			   		    if(Ext.getCmp('leadId') != null){
	   			   		    	var selectedLeadId = Ext.getCmp('leadId').getValue();
	   			   		    	var contactGrd = Ext.getCmp('contactsGrid');
	   			   		    	reloadLeadSubTabs(contactGrd, selectedLeadId);
	   			   		    }
	   			   		    if(Ext.getCmp('customerId') != null){
	   			   		    	var selectedCustomerId = Ext.getCmp('customerId').getValue();
	   			   		    	var contactGrd = Ext.getCmp('contactsGrid');
	   			   		    	reloadCustomerSubTabs(contactGrd, selectedCustomerId);
	   			   		    }
      			   		    //showContacts();
      			   			closeContactWindow();
      			   		  },
      		   failure 	: function(){
      			   			alert("failure");
      			   		  },
      		   jsonData : data   		  
      	   });
		};
		
		function showContactDetails(contactId){
			var formPanel = Ext.getCmp('popUpContactPanel');
			var values = formPanel.getForm().getValues();
			var data = Ext.encode(values);
			
			formPanel.getForm().load({
			method  : 'GET',
	            url     : 'lead/showContactDetailsInPopup?contactId='+contactId,
	      		success : function() {
	            			},
	            failure:function() { 
	                		alert ("Failure");
	               			}
			});
		};
		
		function closeContactWindow(){
			win.close();
		}

})();	